package com.sebiotec.driver.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.allen.library.base.BaseHttpUtil;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.sebiotec.basecommon.utils.constant.SjmConstant;
import com.sebiotec.basecommon.utils.language.LocalManageUtil;
import com.sebiotec.basecommon.utils.tools.ActivityTool;
import com.sebiotec.basecommon.utils.tools.SPUtils;
import com.sebiotec.driver.model.post.JobPostIsRead;
import com.sebiotec.driver.model.post.PostJobDetail;
import com.sebiotec.driver.model.response.JobDetailResponse;
import com.sebiotec.driver.net.BookApiHelper;
import com.sebiotec.driver.ui.view.LoadingDialog;

import java.util.HashMap;

import okhttp3.ResponseBody;

public class JobDetailViewModel extends AndroidViewModel {
    public JobDetailViewModel(@NonNull Application application) {
        super(application);
    }
    public MyJobDetailDataCallBack mDataCallBack;

    //数据回调接口
    public interface MyJobDetailDataCallBack<T> {
        void onDataSuccessCallBack(T t);
        void onDataFailCallBack(T t);
    }
    //设置已读

    public  void  isRead(String id, int Status){

        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
        map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
        map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));
        JobPostIsRead bean =new JobPostIsRead(id,Status);
        BookApiHelper.geRadomApi()
                .JobisRead(map,id,new BaseHttpUtil<JobPostIsRead>().convertVo2Json(bean))
                .compose(Transformer.<ResponseBody>switchSchedulers())
                .subscribe(new CommonObserver<ResponseBody>() {
                    @Override
                    protected String setTag() {
                        return "JobDetail";
                    }

                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(ResponseBody s) {


                    }
                });

    }



    public void  getData(String Bookid, MyJobDetailDataCallBack<Object> mDataCallBack){
        LoadingDialog loadingDialog = new LoadingDialog(ActivityTool.getInstance().getLastActivity());
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
        map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
        map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));

        BookApiHelper.geRadomApi()
                .getJobDetail(map,Bookid)
                .compose(Transformer.<JobDetailResponse>switchSchedulers(loadingDialog))
                .subscribe(new CommonObserver<JobDetailResponse>() {
                    @Override
                    protected String setTag() {
                        return "JobDetail";
                    }
                    @Override
                    protected void onError(String errorMsg) {
                        mDataCallBack.onDataFailCallBack(errorMsg);
                    }

                    @Override
                    protected void onSuccess(JobDetailResponse s) {

                        mDataCallBack.onDataSuccessCallBack(s);
                    }
                });
    }



    public void update(String bookid,int status,MyJobDetailDataCallBack<Object> mDataCallBack){

        LoadingDialog loadingDialog = new LoadingDialog(ActivityTool.getInstance().getLastActivity());
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
        map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
        map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));
        PostJobDetail bean=new PostJobDetail(bookid,status);
        BookApiHelper.geRadomApi()
                .UpdateJob(map,bookid,new BaseHttpUtil<PostJobDetail>().convertVo2Json(bean))
                .compose(Transformer.<ResponseBody>switchSchedulers(loadingDialog))
                .subscribe(new CommonObserver<ResponseBody>() {
                    @Override
                    protected String setTag() {
                        return "JobDetail";
                    }
                    @Override
                    protected void onError(String errorMsg) {
                        mDataCallBack.onDataFailCallBack(errorMsg);
                    }

                    @Override
                    protected void onSuccess(ResponseBody s) {

                        mDataCallBack.onDataSuccessCallBack(s);
                    }
                });

    }

}
